package ua.amsoft.brelok.dao;

import ua.amsoft.brelok.db.DatabaseOpenHelper;
import ua.amsoft.brelok.dto.UserOutputDocsReports;
import ua.amsoft.brelok.manager.DatabaseManager;
import ua.amsoft.brelok.util.BrelokUtils;
import ua.amsoft.brelok.util.DateTimeUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

/**
 * @author Tair Mustafaiev
 */
public class UserOutputDocsReportsDAO implements IUserOutputDocsReportsDAO {
    
    private static final String SELECT_ALL = "select * from " + DatabaseOpenHelper.UODR_TABLE_NAME;
    private static final String TAG = UserOutputDocsReportsDAO.class.getCanonicalName();
    private final DatabaseManager databaseManager;
    
    /**
     * Constructor
     * 
     * @param context {@link Context}
     */
    public UserOutputDocsReportsDAO(final Context context) {
        this.databaseManager = new DatabaseManager(context);
    }
    
    @Override
    public void insert(final ContentValues values) {
        try {
            this.databaseManager.open();
            this.databaseManager.insert(DatabaseOpenHelper.UODR_TABLE_NAME, null, values);
        } finally {
            this.databaseManager.close();
        }
    }
    
    @Override
    public UserOutputDocsReports get() {
        try {
            this.databaseManager.open();
            final String query = UserOutputDocsReportsDAO.SELECT_ALL;
            final Cursor result = this.databaseManager.select(query, null);
            final UserOutputDocsReports reports = BrelokUtils.parseObjectFromCursor(
                    UserOutputDocsReports.class, result, DatabaseOpenHelper.UODR_FILE_COLUMN);
            final String date = result.getString(result
                    .getColumnIndex(DatabaseOpenHelper.UODR_DATE_UPDATED_COLUMN));
            reports.setUpdatedDate(DateTimeUtils.parseOutputDate(date));
            return reports;
        } catch (final Exception e) {
            Log.e(UserOutputDocsReportsDAO.TAG, e.getLocalizedMessage());
        } finally {
            this.databaseManager.close();
        }
        return null;
    }
    
    @Override
    public void update(final ContentValues values, final String whereClause,
            final String[] whereArgs) {
        try {
            this.databaseManager.open();
            this.databaseManager.executeUpdate(DatabaseOpenHelper.UODR_TABLE_NAME, values,
                    whereClause, whereArgs);
        } finally {
            this.databaseManager.close();
        }
    }
    
    @Override
    public void deleteAll() {
        try {
            this.databaseManager.open();
            this.databaseManager.delete(DatabaseOpenHelper.UODR_TABLE_NAME, null, null);
        } finally {
            this.databaseManager.close();
        }
    }
    
    @Override
    public void forceUpdateTable(final ContentValues values) {
        try {
            this.databaseManager.open();
            this.databaseManager.delete(DatabaseOpenHelper.UODR_TABLE_NAME, null, null);
            this.databaseManager.insert(DatabaseOpenHelper.UODR_TABLE_NAME, null, values);
        } finally {
            this.databaseManager.close();
        }
    }
    
}
